<?php

namespace app\sisome\controller;
class Index extends Common
{    //留控
    public function index()
    {
        // die('234');
        //return $this->fetch('hello',['name'=>'thinkphp']);
        //return view('hello',['name'=>'thinkphp']);
        // 方法    说明
        // fetch   渲染模板输出
        // display 渲染内容输出
        // assign  模板变量赋值
        // engine  初始化模板引擎
        $setting = $this->setting;
        $sum['huiyuanall'] = [
            'name' => '总会员数',
            'val' => db('user')->where([])->count(),
            'color' => 'red',
            'icon' => 'im-stats'
        ];
        $level_arr = array(
            //'0' => '未激活',
            '1' => '一级会员',
            '2' => '二级会员',
            '3' => '三级会员',
            '4' => '四级会员',
            '5' => '五级会员'
        );
        $sum['dayall'] = [
            'name' => '当日增加人数',
            'val' => db('user')->where('regtime', 'egt', strtotime(date('Y-m-d')))->count(),
            'color' => 'red',
            'icon' => 'im-stats'
        ];
        $sum['monthall'] = [
            'name' => '当月增加人数',
            'val' => db('user')->where('regtime', 'egt', strtotime(date('Y-m')))->count(),
            'color' => 'red',
            'icon' => 'im-stats'
        ];


        $sum['sumzaixian'] = [
            'name' => '当前活跃人数',
            'val' => db('userget')->where('earnings', '2')->where('toobtain_time', '>', time() - 86400)->group('uname')->count(),
            'color' => 'red',
            'icon' => 'im-stats'
        ];
        $sum['sumid'] = [
            'name' => '实名人数',
            'val' => db('user')->where('shiming', '=', 1)->count(),
            'color' => 'red',
            'icon' => 'im-stats'
        ];
        $sum['sumdongjie'] = [
            'name' => '封号人数',
            'val' => db('user')->where('zt', '=', 2)->count(),
            'color' => 'red',
            'icon' => 'im-stats'
        ];

        foreach ($level_arr as $k => $v) {
            $sum['level' . $k] = [
                'name' => $v,
                'val' => db('user')->where('level', '=', $k)->count(),
                'color' => 'teal',
                'icon' => 'im-stats'
            ];
        }
        //可用idc总量
        $sum['idc_reset'] = [
            'name' => '可用idc',
            'val' => db('user')->sum('idc-idc_freeze'),
            'color' => 'red',
            'icon' => 'im-stats'
        ];
        //当天已完成交易
        $sum['jiaoyi_total'] = [
            'name' => '当天交易总量',
            'val' => db('ppdd_jiaoyi')->where(['zt' => 2])->whereTime('confirm_time', 'today')->sum('num'),
            'color' => 'red',
            'icon' => 'im-stats'
        ];
        //当天转账数量
        $sum['transfer_total'] = [
            'name' => '当天转账总数',
            'val' => db('digital_order')->whereTime('time', 'today')->sum('oid_money'),
            'color' => 'red',
            'icon' => 'im-stats'
        ];
        //当天已完成交易
        $sum['jiaoyi_fee'] = [
            'name' => '当天交易手续费总量',
            'val' => db('ppdd_jiaoyi')->where(['zt' => 2])->whereTime('confirm_time', 'today')->sum('fee'),
            'color' => 'red',
            'icon' => 'im-stats'
        ];
        //当天矿机产出
        $sum['kj_profit'] = [
            'name' => '当天矿机收益总数',
            'val' => db('userget')->where(['earnings' => 2])->whereTime('toobtain_time', 'today')->sum('money'),
            'color' => 'red',
            'icon' => 'im-stats'
        ];
        //总算力
        $sum['total_power'] = [
            'name' => '总算力',
            'val' => db('user')->sum('power'),
            'color' => 'red',
            'icon' => 'im-stats'
        ];
        $this->assign('sum', $sum);
        return view();
    }

    public function setting()
    {  //系统设置
        if (!$_POST) {
            //获取矿机列表
            $kj_list = DB('shop_project')->field('id,name')->select();
            $this->assign('kj_list', $kj_list);
            return view();
        } else {
            // dump($_POST);die();
            unset($_POST['__token__']);
            $num = 0;
            foreach ($_POST as $key => $val) {
                if (is_array($val)) {
                    $val = json_encode($val);
                }
                $ret = db('setting')->where('key', $key)->setField('val', $val);
                $num = $ret ? $num + 1 : $num;
            }
            $this->success('成功更新了 ' . $num . ' 项！');
        }
    }

    public function clearsis()
    {    //清理系统缓存
        // //清除模版缓存 不删除cache目录
        // Cache::clear();

        //清除模版缓存 不删除 temp目录
        array_map('unlink', glob(TEMP_PATH . DS . '*.php'));

        //清除日志缓存/runtime/log 不删除log目录
        $path = glob(LOG_PATH . '*');
        foreach ($path as $item) {
            array_map('unlink', glob($item . DS . '*.log'));
            rmdir($item);
        }

        return '清理成功';
    }
}
